package com.sanji.sjmallmodule.goods;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sanji.sjmallapp.R;
import com.sanji.sjmallwidget.AmountView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by fhm on 2016/12/30.
 * 商品选择数量加入购物车
 */

public class GoodsModelAdapter extends BaseAdapter {
    private List<GoodsBean> mData;
    private Context mContex;
    private buyGoodsNumberListener listener;

    public GoodsModelAdapter(List<GoodsBean> mData, Context mContex) {
        this.mData = mData;
        this.mContex = mContex;
    }

    public void setOnBuyGoodsNumberListener(buyGoodsNumberListener listener) {
        this.listener = listener;
    }

    public void notifyAdapter(List<GoodsBean> mData) {
        this.mData = mData;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public GoodsBean getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vHolder;
        if (view == null) {
            view = LayoutInflater.from(mContex).inflate(R.layout.activity_goods_detail_pop_item, null);
            vHolder = new ViewHolder(view);
            view.setTag(vHolder);
        } else {
            vHolder = (ViewHolder) view.getTag();
        }
        //添加数据
        final GoodsBean goodsBean = mData.get(i);
        vHolder.popItemGoodsMoney.setText("￥" + goodsBean.getPhonePrice());
        //显示商品的颜色
        StringBuilder sb = new StringBuilder();
        sb.append((goodsBean.getStandard().equals("null")) ? "   " : "   " + goodsBean.getStandard())
                .append((goodsBean.getEdition().equals("null")) ? "  " : "   " + goodsBean.getEdition())
                .append((goodsBean.getColor().equals("null")) ? "   " : "   " + goodsBean.getColor());

        vHolder.popItemGoodsName.setText(sb.toString());

        vHolder.ramTextView.setText(goodsBean.getStorage());

        //是否有存货
        vHolder.popItemHaveGoods.setText(goodsBean.getStock() == 0 ? "无货" : "有货");
        //设置存货数量
        vHolder.popItemSelectNumber.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
//                int pos = (int) view.getTag();
                goodsBean.setBuyNumber(amount);
                if (listener != null) {
                    listener.buyGoodsNumber();
                }
            }
        });
        vHolder.popItemSelectNumber.setGoodsStorage(goodsBean.getStock(), goodsBean.getLimitCount(), goodsBean.getBuyNumber());
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.pop_item_goods_name)
        TextView popItemGoodsName;
        @BindView(R.id.pop_item_goods_money)
        TextView popItemGoodsMoney;
        @BindView(R.id.pop_item_have_goods)
        TextView popItemHaveGoods;
        @BindView(R.id.pop_item_select_number)
        AmountView popItemSelectNumber;
        @BindView(R.id.pop_item_ram)
        TextView ramTextView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface buyGoodsNumberListener {
        public void buyGoodsNumber();
    }
}
